/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lazydatamodels;

import entities.Music;
import java.util.Comparator;
import org.primefaces.model.SortOrder;
import util.Introspection;

/**
 *
 * @author Antoine Boulinguez et Shyn-Yuan Cheng
 */
public class LazySorter implements Comparator<Music> {

    private String sortField;
    private SortOrder sortOrder;

    public LazySorter(String sortField, SortOrder sortOrder) {
        this.sortField = sortField;
        this.sortOrder = sortOrder;
    }

    @Override
    public int compare(Music m1, Music m2) {
        try {
            // Get property values to sort
            String value1 = Introspection.getFieldFromObject(m1, sortField);
            String value2 = Introspection.getFieldFromObject(m2, sortField);

            // Compare the values depending on the sort order
            if (SortOrder.ASCENDING.equals(sortOrder)) {
                return value2.toLowerCase().compareTo(value1.toLowerCase());
            } else {
                return value1.toLowerCase().compareTo(value2.toLowerCase());
            }

        } catch (Exception e) {
            throw new RuntimeException();
        }
    }
}